from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random


class Picture(object):
    def __init__(self, text_str, size, background):
        # text_str: 验证码显示的字符组成的字符串
        # size:  图片大小
        # background: 背景颜色
        self.text_list = list(text_str)
        self.size = size
        self.background = background

    def create_pic(self):
        # 创建一张图片
        self.width, self.height = self.size
        self.img = Image.new("RGB", self.size, self.background)
        # 实例化画笔
        self.draw = ImageDraw.Draw(self.img)

    def create_point(self, num, color):
        # num: 画点的数量
        # color: 点的颜色
        # 功能：画点
        for i in range(num):
            self.draw.point(
                (random.randint(0, self.width), random.randint(0, self.height)),
                fill=color
            )

    def create_line(self, num, color):
        # num: 线条的数量
        # color: 线条的颜色
        # 功能：画线条
        for i in range(num):
            self.draw.line(
                [
                    (random.randint(0, self.width), random.randint(0, self.height)),
                    (random.randint(0, self.width), random.randint(0, self.height))
                ],
                fill=color
            )

    def create_text(self, font_type, font_size, font_color, font_num, start_xy):
        """
        font_type: 字体
        font_size: 文字大小
        font_color: 文字颜色
        font_num:  文字数量
        start_xy:  第一个字左上角坐标,元组类型，如 (5,5)
        功能： 画文字
        """
        font = ImageFont.truetype(font_type, font_size)
        code = " ".join(random.sample(self.text_list, font_num))
        print(code.replace(" ", ''))
        self.draw.text(start_xy, code, font=font, fill=font_color)

    def opera(self):
        """
        功能：给画出来的线条，文字，扭曲一下，缩放一下，位移一下，滤镜一下。
        就是让它看起来有点歪，有点扭。
        """
        params = [
            1 - float(random.randint(1, 2)) / 100,
            0,
            0,
            0,
            1 - float(random.randint(1, 10)) / 100,
            float(random.randint(1, 2)) / 500,
            0.001,
            float(random.randint(1, 2)) / 500
        ]
        self.img = self.img.transform(self.size, Image.PERSPECTIVE, params)
        self.img = self.img.filter(ImageFilter.EDGE_ENHANCE_MORE)


def genenter():
    strings = "abcdefghjkmnpqrstwxyz023456789ABCDEFGHJKLMNPQRSTWXYZ"
    size = (120, 40)
    background = 'white'
    pic = Picture(strings, size, background)
    pic.create_pic()
    pic.create_point(300, (220, 220, 220))
    pic.create_line(20, (220, 220, 220))
    pic.create_text("simsun.ttc", 24, (0, 0, 205), 4, (7, 7))
    pic.opera()
    pic.img.show()


def validate_picture():
    total = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345789'
    # 图片大小120 x 40
    width = 120
    heighth = 40
    # 先生成一个新图片对象
    im = Image.new('RGB', (width, heighth), 'white')
    # 设置字体
    font = ImageFont.truetype("simsun.ttc", 24)
    # 创建draw对象
    draw = ImageDraw.Draw(im)
    str = ''
    # 输出每一个文字
    for item in range(4):
        text = random.choice(total)
        str += text
        draw.text((5+random.randint(4, 7)+20*item, 5+random.randint(3, 7)), text=text, fill=(0, 0, 205), font=font)

    # 划几根干扰线
    for num in range(8):
        x1 = random.randint(0, width/2)
        y1 = random.randint(0, heighth/2)
        x2 = random.randint(0, width)
        y2 = random.randint(heighth/2, heighth)
        draw.line(((x1, y1), (x2, y2)), fill='black', width=1)

    # 模糊下,加个帅帅的滤镜～
    im = im.filter(ImageFilter.FIND_EDGES)
    print(str)
    im.show()
    return im, str


if __name__ == "__main__":
    validate_picture()

